import { defineEventHandler, sendProxy, readBody, H3Event } from "h3";

const normalizeBody = async (evt: H3Event) => {
  if (evt.req.method === "GET") return;
  const body = await readBody(evt);
  if (typeof body === "string") return body;
  if (typeof body === "object" && body !== null) {
    return JSON.stringify(body);
  }
  return String(body);
};
export default defineEventHandler(async evt => {
  const headers = evt.req.headers as Record<string, string>;
  delete headers["host"];
  return await sendProxy(
    evt,
    import.meta.env.VITE_DISCUZ_LOCAL_URL + evt.req.url,
    {
      fetchOptions: {
        method: evt.req.method,
        body: await normalizeBody(evt),
        headers
      },
      sendStream: true
    }
  );
});
